package com.ciwong.mobilelib.eyeguard;

import android.content.Context;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.os.Build;
import android.util.Log;
import android.view.Gravity;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.LinearLayout;

import com.ciwong.mobilelib.application.BaseApplication;

public class EyeGuardModeView {
    private static final String TAG = "EyeGuardModeView";
    private static LinearLayout sNightView;

    public static void showEyeGuard(boolean isOpenEyeGuardMode) {
        if (isOpenEyeGuardMode) {
            if (sNightView == null) {
                // 保护眼睛模式的核心代码
                try {
                    WindowManager localWindowManager = (WindowManager) BaseApplication.getInstance().getSystemService(Context.WINDOW_SERVICE);
                    LayoutParams localLayoutParams = new WindowManager.LayoutParams();
                    sNightView = new LinearLayout(BaseApplication.getInstance());
                    //不让悬浮窗获取焦点
                    sNightView.setFocusable(false);
                    sNightView.setFocusableInTouchMode(false);
                    if (Build.VERSION.SDK_INT >= 26) {
                        // 注意TYPE_SYSTEM_ALERT从Android8.0开始被舍弃了
                        localLayoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
                    } else {
                        // 从Android8.0开始悬浮窗要使用TYPE_APPLICATION_OVERLAY
                        localLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
                    }
                    localLayoutParams.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL
                            | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                            | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
                            | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
                    //支持透明度
                    localLayoutParams.format = PixelFormat.RGBA_8888;
                    localLayoutParams.gravity = Gravity.TOP;
                    localLayoutParams.x = 0;
                    localLayoutParams.y = 0;
                    localLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
                    localLayoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;
                    localWindowManager.addView(sNightView, localLayoutParams);
                    sNightView.setBackgroundColor(getColor2());
                } catch (Exception e) {
                    Log.e(TAG, "showEyeGuard: " + e.getMessage());
                }

            }
        } else {
            if (sNightView != null) {
                try {
                    WindowManager localWindowManager = (WindowManager) BaseApplication.getInstance().getSystemService(Context.WINDOW_SERVICE);
                    localWindowManager.removeView(sNightView);
                    sNightView = null;
                } catch (Exception e) {

                }
            }
        }
    }

    public static int getColor2() {

        return Color.parseColor("#33ffb400");
    }

    public static int getColor3() {
        int realFilter = 50;
        if (realFilter < 10) {
            realFilter = 10;
        } else if (realFilter > 80) {
            realFilter = 80;
        }
        int a = (int) (realFilter / 80f * 180);
        int r = (int) (200 - (realFilter / 80f) * 190);
        int g = (int) (180 - (realFilter / 80f) * 170);
        int b = (int) (60 - realFilter / 80f * 60);
        return Color.argb(a, r, g, b);
    }

}
